package controllers;

import java.net.URL;

import models.Usuario;
import play.mvc.Controller;
import siena.Json;
import util.AuthUtil;

import com.google.appengine.api.urlfetch.HTTPResponse;
import com.google.appengine.api.urlfetch.URLFetchService;
import com.google.appengine.api.urlfetch.URLFetchServiceFactory;

public class Twitter extends Controller {
	public static void index() throws Exception {
		Usuario objUsuario = AuthUtil.getUsuario();

		String serviceUrl = AuthUtil.getConnector().sign(objUsuario,
				"http://api.twitter.com/1/account/verify_credentials.json");

		response.contentType = "text/plain";

		URLFetchService urlFetchService = URLFetchServiceFactory
				.getURLFetchService();

		HTTPResponse httpResponse = urlFetchService.fetch(new URL(serviceUrl));

		String jsonResult = new String(httpResponse.getContent(), "UTF-8");

		Json jsonObject = Json.loads(jsonResult);

		renderText(jsonObject + "\n" + jsonObject.get("screen_name").asString());
	}
}
